<meta http-equiv="content-type" content="text/html;" charset="utf8" />
<html>
		<body>


                <!--<SCRIPT LANGUAGE="JavaScript">
                
                function checkAll(field)
                {
                for (i = 0; i < field.length; i++)
                        field[i].checked = true ;
                }
                
                function uncheckAll(field)
                {
                for (i = 0; i < field.length; i++)
                        field[i].checked = false ;
                }
                
                </script>

                -->

		<form name="myform" action="" method="GET">
			<label for="table">Tabela: &nbsp;</label>
			<input type="text" id="table" name="table" value="<?php echo $_GET["table"];?>"/>
			<input type="submit" value="Gerar"/>
			<a href=".">Reset</a>
		<pre>
		
		<?php
		if ($_GET['submit'] == 'index') {
				echo 'Arquivo index gerado!<br>';
		}
		if ($_GET['submit'] == 'edit') {
			echo 'Arquivo edit gerado!<br>';
		}
		if ($_GET['submit'] == 'view') {
			echo 'Arquivo view gerado!<br>';
		}
		if ($_GET['submit'] == 'add') {
			echo 'Arquivo add gerado!<br>';
		}
		if ($_GET['submit'] == 'controller') {
			echo 'Arquivo controller gerado!<br>';
		}
		function pr($str){
				
			return htmlentities($str."\n");
		}
		
		function plural($str) {
			if (substr($str,-1,1) == 's')
				return $str;
			else 
				return $str."s";
		}

		function singular($str) {
			if (substr($str,-1,1) == 's')
				return substr($str,0,strlen($str)-1);
			else 
				return $str;
		}
		
		function array_search_str($str, $array_str) {
			if (!isset($array_str))
				return false;
			foreach ($array_str as $row) {
				if ($row == $str)
					return true;
			}
			return false;
		}
		/* Configuracao do banco de dados */
		$username = "mc926";
		$password = "mc926";
		$hostname = "localhost";	
		$con = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
		mysql_select_db("mc926", $con);
		
		/* Recuperar o nome da tabela */
		$table = $_GET["table"];
		
		/* inicializacao dos vetores da selecao de campos */
		$column = array();
		$view = array();
		$addc = array();
		$edit = array();
		
		/* recupera a selecao dos campos */
		if (isset($_GET["ind"])) {
			foreach ($_GET["ind"] as $c)
				$column[] = $c;
		}
		if (isset($_GET["vie"])) {
			foreach ($_GET["vie"] as $c)
				$viewc[] = $c;
		}
		if (isset($_GET["add"])) {
			foreach ($_GET["add"] as $c)
				$addc[] = $c;
		}
		if (isset($_GET["edi"])) {
			foreach ($_GET["edi"] as $c)
				$edit[] = $c;
		}
						
		if (!empty($table)) {
			/* Trata o nome da classe em diferentes formas */
			$str_maiuscula = explode("_", $table);
			
			
			$i = 0;
			foreach ($str_maiuscula as $maiuscula) {
				$str_maiuscula[$i++] = plural(ucfirst($maiuscula));
			}
			
			$strUP = implode("", $str_maiuscula);			//Classes
			
			$i = 0;
			foreach ($str_maiuscula as $maiuscula) {
				$str_maiuscula[$i++] = singular(ucfirst($maiuscula));
			}
			
			print_r($str_maiusculas);
			$strUS = implode("", $str_maiuscula);		//Classe
			
			
			$strDS = substr($table,0,strlen($table)-1);		//classe
			$strDP = $table;								//classes

			/* Tratamento de maiuscula*/
			
			$strUS = str_replace("_", " ", $strUS);
			
			/* Busca os campos da tabela */
			$campos = array();
			$query = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name ='$table'";
			echo "<h3>".$table."</h3>";
			$result = mysql_query($query,$con);
			if (mysql_num_rows($result) == 0)
				die("Tabela inexistente!");
			while ($row = mysql_fetch_array($result)) {
				$campos[] = $row['COLUMN_NAME'];
			}
			
			/* Imprime as opcoes de campos */
			echo  "\nSelecione para a tabela:\n";
			echo "<table border='1'>";
			echo "<tr><td>";
			echo "index</a><br>";
			foreach ($campos as $campo) {
				if (array_search_str($campo, $column)!=false) $checked = " checked ";
				else $checked = "";
				echo "<input type='checkbox' name='ind[]' id='index' $checked value='$campo'>$campo <br>";
			}
		    //SUPER MEGA CHECADOR DO EVD
		   // echo "<input type=\"button\" name=\"CheckAll\" value=\"Check All\" onClick=\"checkAll(document.myform.ind[])\">";
                   // echo "<br> <input type=\"button\" name=\"UnCheckAll\" value=\"Uncheck All\" onClick=\"uncheckAll(document.myform.ind[])\"> <br> ";
                                            
			echo "<input type='submit' name='submit' value='index' />";
			echo "</td><td>";
			
			
			
			echo "view<br>";
			foreach ($campos as $campo) {
				if (array_search_str($campo, $viewc)!=false) $checked = " checked ";
				else $checked = "";
				echo "<input type='checkbox' name='vie[]' id='view' $checked value='$campo'>$campo <br>";
			}
			// SUPER MEGA CHECADOR DO EVD
		   // echo "<input type=\"button\" name=\"CheckAll\" value=\"Check All\" onClick=\"checkAll(document.myform.vie)\">";
                    //echo "<br> <input type=\"button\" name=\"UnCheckAll\" value=\"Uncheck All\" onClick=\"uncheckAll(document.myform.vie)\"> <br> ";                    
			echo "<input type='submit' name='submit' value='view' />";
			echo "</td><td>";
			
			
			echo "add<br>";
			foreach ($campos as $campo) {
				if (array_search_str($campo, $addc)!=false) $checked = " checked ";
				else $checked = "";
				echo "<input type='checkbox' name='add[]' id='add' $checked value='$campo'>$campo <br>";
			}
			// SUPER MEGA CHECADOR DO EVD
		   // echo "<input type=\"button\" name=\"CheckAll\" value=\"Check All\" onClick=\"checkAll(document.myform.add)\">";
                   // echo "<br> <input type=\"button\" name=\"UnCheckAll\" value=\"Uncheck All\" onClick=\"uncheckAll(document.myform.add)\"> <br> ";
			echo "<input type='submit' name='submit' value='add' />";
			echo "</td><td>";
			
			
			echo "edit<br>";
			foreach ($campos as $campo) {
				if (array_search_str($campo, $edit)!=false) $checked = " checked ";
				else $checked = "";
				echo "<input type='checkbox' name='edi[]' id='edit' $checked value='$campo'>$campo <br>";
			}
			// SUPER MEGA CHECADOR DO EVD
		  //  echo "<input type=\"button\" name=\"CheckAll\" value=\"Check All\" onClick=\"checkAll(document.myform.edi)\">";
                  //  echo "<br> <input type=\"button\" name=\"UnCheckAll\" value=\"Uncheck All\" onClick=\"uncheckAll(document.myform.edi)\"> <br> ";
			echo "<input type='submit' name='submit' value='edit' />";
			echo "</td><td>
					<input type='submit' name='submit' value='controller' />
					</td></tr></table>";
			echo "<br />";
			echo "<hr>";
			if (!empty($column)) {
				include_once("index_gen.php");
			}

			if (!empty($viewc)) {
				include_once("view_gen.php");
			}
				
			if (!empty($addc)) {
				include_once("add_gen.php");
			}
				
			if (!empty($edit)) {
				include_once("edit_gen.php");
			}

			include_once("controller_gen.php");
		}

		?>
				</form>	
				</pre>
				
			      
				
				</body>
			</html>